/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: �������ƿƼ����޹�˾</p>
 * <p>Aug 8, 2012 2:36:22 PM</p>
 * @author wangyunpeng
 * @version 1.0
 */
package com.ce.utils;

/** 
 * desc:
 * <p>�����ˣ�wangyunpeng �������ڣ�Aug 8, 2012 </p>
 * @version V1.0  
 */
public class Formatter{
    public Formatter()
    {
    }

    public static String fmtNumR(long num)
    {
        return ralign(String.valueOf(num), 8, ' ');
    }

    public static String fmtNumL(long num)
    {
        return lalign(String.valueOf(num), 8, ' ');
    }

    public static String fmtStrR(String str, int len)
    {
        if(str.getBytes().length >= len)
            return str;
        else
            return ralign(str, len, ' ');
    }

    public static String fmtStrL(String str, int len)
    {
        if(str.getBytes().length >= len)
            return str;
        else
            return lalign(str, len);
    }

    public static String ltrim(String str, int len)
    {
        byte bstr[] = str.getBytes();
        if(bstr.length <= len)
        {
            return str;
        } else
        {
            byte obs[] = new byte[len];
            int offset = bstr.length - len;
            System.arraycopy(bstr, offset, obs, 0, len);
            return new String(obs);
        }
    }

    public static String rtrim(String str, int len)
    {
        byte bstr[] = str.getBytes();
        if(bstr.length <= len)
        {
            return str;
        } else
        {
            byte obs[] = new byte[len];
            System.arraycopy(bstr, 0, obs, 0, len);
            return new String(obs);
        }
    }

    public static String ralign(String str, int len)
    {
        return ralign(str, len, '0');
    }

    public static String lalign(String str, int len)
    {
        return lalign(str, len, ' ');
    }

    public static String ralign(String str, int len, char fill)
    {
        return align(str, len, fill, false);
    }

    public static String lalign(String str, int len, char fill)
    {
        return align(str, len, fill, true);
    }

    public static String align(String str, int len, char fill, boolean leftAlign)
    {
        if(str == null)
            str = "";
        byte buf[] = new byte[len];
        byte bstr[] = str.getBytes();
        int str_len = bstr.length;
        int fill_len = len - str_len;
        if(leftAlign)
        {
            if(fill_len > 0)
                System.arraycopy(bstr, 0, buf, 0, str_len);
            else
                System.arraycopy(bstr, 0, buf, 0, len);
        } else
        if(fill_len > 0)
            System.arraycopy(bstr, 0, buf, fill_len, str_len);
        else
            System.arraycopy(bstr, -fill_len, buf, 0, len);
        for(int i = 0; i < fill_len; i++)
            if(leftAlign)
                buf[i + str_len] = (byte)fill;
            else
                buf[i] = (byte)fill;

        return new String(buf);
    }

    public static String trim(String str)
    {
        return str != null ? str.trim() : "";
    }
}
